home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / afloat.zip / FBIN2DEC.ASM < prev    next >
Assembly Source File  |  1988-03-15  |  3KB  |  150 lines

  1.         PAGE ,132
  2. ;----------------------------------------------------------
  3. ; FBINTODEC
  4. ;
  5. ; Copyright Bob Kline 1988
  6. ;
  7. ; Purpose:
  8. ;    convert binary float to decimal components
  9. ;
  10. ; Input:
  11. ;       DX:AX contains binary floating point value
  12. ;       in IEEE format
  13. ;
  14. ; Output:
  15. ;       DX:AX contains mantissa with decimal point
  16. ;       understood to be at end; good to 7-8 places
  17. ;       of precision; CX contains sign in low bit;
  18. ;       BX contains signed, unbiased decimal exponent
  19. ;
  20. ; Other registers changed:
  21. ;       DI, SI
  22. ;
  23. ; Example:
  24. ;       1.234567e6 (1,234,567) input in DX:AX as
  25. ;       32-bit value: bit 31 is sign, bits 30-23
  26. ;       are the 8-bit biased binary exponent, and
  27. ;       the lowest 23 bits are the mantissa, with
  28. ;       a 24th (high) bit understood to be set.
  29. ;
  30. ;       Output would be 1234567 in DX:AX, -4 in
  31. ;       BX, and 0 in CX.
  32. ;
  33. ; Other procedures called:
  34. ;       LDIV10 -- divide 32-bit unsigned integer by 10
  35. ;----------------------------------------------------------
  36.         PUBLIC  FBINTODEC
  37.         EXTRN   LDIV10:PROC
  38.  
  39.     .MODEL    SMALL
  40.  
  41.     .CODE
  42.  
  43. FBINTODEC PROC
  44.  
  45. ; isolate the sign flag and save it
  46.         MOV     CX,DX
  47.         ROL     CX,1
  48.         AND     CX,1
  49.         PUSH    CX
  50.  
  51. ; keep the exponent in SI for now, and remove bias
  52.         MOV     SI,DX
  53.         AND     SI,7FFFH
  54.         MOV     CL,7
  55.         SHR     SI,CL
  56.         SUB     SI,127
  57.  
  58. ; isolate the mantissa and restore the high bit
  59.         AND     DX,7FH
  60.         OR      DX,80H
  61.  
  62. ; start out the decimal exponent with 0
  63.         XOR     DI,DI
  64.  
  65. ; if this is a big number slide the mantissa up
  66.         CMP     SI,32
  67.     JL    LOWEXP
  68.         MOV     CX,8
  69. SHFT1:  SHL     AX,1
  70.         RCL     DX,1
  71.         LOOP    SHFT1
  72.  
  73. ; while the exponent is greater than 31 divide by 10
  74.         JMP     SHORT TEST1
  75. LOOP1:  CALL    LDIV10
  76.         XOR     CX,CX   ; shift count
  77.  
  78. ; keep shifting the mantissa left until high bit set
  79.         JMP     SHORT TEST2
  80. LOOP2:  SHL     AX,1
  81.         RCL     DX,1
  82.         INC     CX      ; shift count
  83. TEST2:  TEST    DX,8000H
  84.         JZ      LOOP2
  85.  
  86. ; fold the remainder (BX) back into the mantissa,
  87. ;   reduce the binary exponent by the number of
  88. ;   shifts we made, and bump up the decimal exponent
  89.         OR      AX,BX
  90.         SUB     SI,CX
  91.         INC     DI
  92.  
  93. ; is the exponent > 31 ?
  94. TEST1:    CMP    SI,31
  95.     JG    LOOP1
  96.  
  97. ; normalize the mantissa
  98.         MOV     CX,31
  99.         SUB     CX,SI
  100.     JZ    DONE
  101. SHFT2:    SHR    DX,1
  102.     RCR    AX,1
  103.         LOOP    SHFT2
  104.         JMP     SHORT DONE
  105.  
  106. LOWEXP:
  107. ; multiply by 10 and shift while exponent
  108. ;   is less than 23
  109.         JMP     SHORT TEST3
  110. LOOP3:  MOV     CX,10
  111.         PUSH    AX
  112.         MOV     AX,DX
  113.         MUL     CX
  114.         MOV     BX,AX
  115.         POP     AX
  116.         MUL     CX
  117.         ADD     DX,BX
  118.         XOR     CX,CX   ; shift counter
  119.         JMP     SHORT TEST4
  120. LOOP4:  SHR     DX,1
  121.         RCR     AX,1
  122.         INC     CX      ; shift count
  123. TEST4:  TEST    DX,0F000H
  124.         JNZ     LOOP4
  125.  
  126. ; adjust binary (SI) and decimal (DI) exponents
  127.         ADD     SI,CX
  128.         DEC     DI
  129.  
  130. TEST3:  CMP     SI,23
  131.     JL    LOOP3
  132.  
  133. ; normalize mantissa
  134.         SUB     SI,23
  135.     JZ    DONE
  136.         MOV     CX,SI
  137. SHFT3:  SHL     AX,1
  138.         RCL     DX,1
  139.         LOOP    SHFT3
  140.  
  141. DONE:
  142. ; move the exponent to BX and get the sign back
  143.         MOV     BX,DI
  144.         POP     CX
  145.         RET
  146.  
  147. FBINTODEC ENDP
  148.  
  149.         END
  150.